﻿using System;
using System.Collections;
using System.Collections.Generic;

using IBatisNet.DataAccess;

using AUV5.Model;
using AUV5.DAL;
using AUV5.DAL.Interface;
using AUV5.DAL.Dao;

namespace AUV5.BLL
{
    /// <summary>
    /// 功能：     Favorite业务逻辑实现
    /// 作者：     lwl
    /// 日期：     2011/10/14
    /// 修改日期： 
    /// 修改人：
    /// 修改内容：
    /// </summary>
    public class FavoriteBLL
    {
        private static FavoriteBLL instance = null;
        private static object syncRoot = new object();
        private IDaoManager daoManager = ServiceConfig.Instance.DaoManager;
        private IFavoriteDao dao = null;

        private FavoriteBLL()
        {
            dao = FavoriteSqlMapDao.Instance;
        }

        public static FavoriteBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new FavoriteBLL();
                        }
                    }
                }
                return instance;
            }
        }

        /// <summary>
        /// 得到最大行数
        /// </summary>
        /// <returns></returns>
        public int GetMaxCounts()
        {
            try
            {
                //daoManager.BeginTransaction();

                int count = dao.GetMaxCounts();

                //daoManager.CommitTransaction();

                return count;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public System.Collections.Generic.IList<Favorite> GetList()
        {
            try
            {
                //daoManager.BeginTransaction();

                System.Collections.Generic.IList<Favorite> list = dao.GetList();

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private System.Collections.Generic.IList<Favorite> GetListByParameters(Favorite favorite)
        {
            try
            {
                //daoManager.BeginTransaction();
                System.Collections.Generic.IList<Favorite> list = dao.GetListByParameters(favorite);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }


        /// <summary>
        /// 得到明细
        /// </summary>
        /// <param name="FavoriteID"></param>
        /// <returns></returns>
        public Favorite GetDetail(System.String favoriteid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.GetDetail(favoriteid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }


        /// <summary>
        /// 加入收藏
        /// </summary>
        /// <param name="favorite"></param>
        /// <returns></returns>
        public object Add(Favorite favorite)
        {

            try
            {
                daoManager.BeginTransaction();

                favorite.State = (int)Common.Enum.auvState.Enabled;
                favorite.IsDel = (int)Common.Enum.auvDel.NotDel;
                favorite.CreateDate = DateTime.Now;
                favorite.UpdateDate = DateTime.Now;
                favorite.BeginDate = DateTime.Now;
                favorite.EndDate = DateTime.Now;

                object obj = dao.Add(favorite);

                daoManager.CommitTransaction();

                return obj;
            }
            catch
            {
                daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="favorite"></param>
        /// <returns></returns>
        public int Update(Favorite favorite)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Update(favorite);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="FavoriteID"></param>
        /// <returns></returns>
        public int Delete(System.String favoriteid)
        {
            try
            {
                ///daoManager.BeginTransaction();
                Favorite fav = new Favorite();
                fav.FavoriteID = favoriteid;
                return dao.Delete(fav);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="favorite"></param>
        /// <returns></returns>
        public int Delete(Favorite favorite)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(favorite);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 判断某服务是否已被当前用户收藏
        /// </summary>
        /// <param name="serviceID"></param>
        /// <returns></returns>
        public bool Contain(string serviceID,string userID)
        {
            try
            {
                Favorite favorite = new Favorite();
                favorite.ServiceID = serviceID;
                favorite.UserID = userID;
                return dao.GetListByParameters(favorite).Count > 0;
            }
            catch 
            {
                
                throw;
            }
        }

        /// <summary>
        /// 获取某用户收藏的服务列表
        /// </summary>
        /// <returns></returns>
        public IList<ServiceInfo> GetPagedFavoriteServiceList(NetPager pager)
        {
            try
            {
                IList<Favorite> list=dao.GetPagedList(pager);

                return ServiceInfoBLL.Instance.GetServiceByFavoriteList(list);
            }
            catch 
            {
                throw;
            }
        }

        #region##通过服务ID得到被收藏数
        /// <summary>
        /// 通过服务ID得到被收藏数
        /// 创建人：Wilson
        /// 创建时间：2011-10-27
        /// </summary>
        /// <param name="serviceId"></param>
        /// <returns></returns>
        public int GetSFavCountByServiceId(string serviceId)
        {
            try
            {
                return dao.GetSFavCountByServiceId(serviceId);
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
    }
}
